<?php
namespace Framework\Library\Validator;

use Phalcon\Validation;
use Phalcon\Validation\Validator;
use Phalcon\Validation\Message;
use Phalcon\Validation\ValidatorInterface;

class Int64Validator extends Validator implements ValidatorInterface
{
    public function validate(Validation $validation, $attribute)
    {
        $minimum = (int) ($this->getOption('minimum') ?? $this->getOption('min') ?? 1);
        $maximum = (int) ($this->getOption('maximum') ?? $this->getOption('max') ?? PHP_INT_MAX);

        $value = $validation->getValue($attribute);
        if (intval($value) . '' !== $value) {
            $validation->appendMessage(new Message('Int64 format error', $attribute, 'Int64'));

            return false;
        }
        if ($value < $minimum || $value > $maximum) {
            $validation->appendMessage(new Message('Int64 is not in a valid range', $attribute, 'Int64'));

            return false;
        }

        return true;
    }
}
